/***     
 * 查找同花色的牌
 */

var _ = require('../Underscore');
var FindCardTool = require('./FindCardTool');
var CardType = require('../CardType');
var CardTool = require('../CardTool');
var FindSameCardTypeTool = module.exports = {};

/***
 * 获得相同牌数量
 * _cardInfoList:[{"cardId":64,"type":2,"value":12},{"cardId":22,"type":2,"value":9}]//牌信息
 * cardType:花色
 */
FindSameCardTypeTool.getResult = function(_cardInfoList, cardType) {
    var result = FindCardTool.getResult(_cardInfoList, function(_cardInfo) {
        return cardType == _cardInfo.type;
    }, function(_cardInfo) {
        return cardType != _cardInfo.type;
    });
    return result;
};

/***
 * 
 */
FindSameCardTypeTool.getMoreResultA = function(_cardInfoList) {
    var findGroupMore = [];
    var otherGroup = [];

    var group = this.getGroupTypeList(_cardInfoList);
    _.each(group, function(_singleGroup) {
        var resultInfo = this.getResultA(_singleGroup);
        if (resultInfo.isFind) {
            var info = {
                findGroup: resultInfo.findGroup,
                otherGroup: _.difference(_cardInfoList, resultInfo.findGroup)
            }
            findGroupMore.push(info);
        } else {
            otherGroup.push(resultInfo.otherGroup);
        }
    }.bind(this));

    var result = {
        isFind: _.size(findGroupMore) > 0,
        findGroupMore: findGroupMore
    }
    return result;
};

/***
 * 只找A同花
 */
FindSameCardTypeTool.getResultA = function(_cardInfoList) {
    var size = _.size(_cardInfoList);
    if (size < 5) {
        return {
            isFind: false,
            findGroup: [],
            otherGroup: _cardInfoList
        }
    }

    _cardInfoList = _.sortBy(_cardInfoList, function(_cardInfo) {
        return _cardInfo.value;
    });

    var isHaveA = _cardInfoList[0].value == 1;

    var tmpCardList = _.partition(_cardInfoList, function(cardInfo, idx) {
        return idx <= 4;
    });
    return {
        isFind: true,
        findGroup: tmpCardList[0],
        otherGroup: tmpCardList[1],
    };
};

/***先将4个花色分出来 */
FindSameCardTypeTool.getResultAll = function(_cardInfoList) {
    var result = {
        findGroup: [],
        otherGroup: []
    }
    var resultInfo = this.getResult(_cardInfoList, CardType.hei);
    if (resultInfo.isFind) {
        result.isFind = true;
        result.findGroup.push(resultInfo.findGroup);
    }
    resultInfo = this.getResult(resultInfo.otherGroup, CardType.hong);
    if (resultInfo.isFind) {
        result.isFind = true;
        result.findGroup.push(resultInfo.findGroup);
    }
    resultInfo = this.getResult(resultInfo.otherGroup, CardType.mei);
    if (resultInfo.isFind) {
        result.isFind = true;
        result.findGroup.push(resultInfo.findGroup);
    }
    resultInfo = this.getResult(resultInfo.otherGroup, CardType.fang);
    if (resultInfo.isFind) {
        result.isFind = true;
        result.findGroup.push(resultInfo.findGroup);
    }
    result.otherGroup = resultInfo.otherGroup;
    // console.error('拆分花色结果:', JSON.stringify(result));
    return result;
};

/***分类别*/
FindSameCardTypeTool.getGroupTypeList = function(_cardInfoList) {
    var resultInfo = this.getResult(_cardInfoList, CardType.hei);
    var group = [];
    if (resultInfo.isFind) {
        group.push(resultInfo.findGroup);
    }
    resultInfo = this.getResult(resultInfo.otherGroup, CardType.hong);
    if (resultInfo.isFind) {
        group.push(resultInfo.findGroup);
    }
    resultInfo = this.getResult(resultInfo.otherGroup, CardType.mei);
    if (resultInfo.isFind) {
        group.push(resultInfo.findGroup);
    }
    resultInfo = this.getResult(resultInfo.otherGroup, CardType.fang);
    if (resultInfo.isFind) {
        group.push(resultInfo.findGroup);
    }
    return group;
};